const { ccclass, property } = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {

    @property(cc.Camera)
    camera: cc.Camera = null;

    @property(cc.Sprite)
    box: cc.Sprite = null;

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {}

    start() {

        // this.scheduleOnce(() => {
        //     this.initdata();
        // }, 2);
        this.node.x = cc.winSize.width / 2;
        this.node.y = cc.winSize.height / 2;

        this.camera.zoomRatio = 0.025;
        this.initdata()
    }


    initdata() {



        let node = new cc.Node();
        node.parent = cc.director.getScene();
        let camera = this.camera

        // 设置你想要的截图内容的 cullingMask
        // camera.cullingMask = 0xffffffff;

        // 新建一个 RenderTexture，并且设置 camera 的 targetTexture 为新建的 RenderTexture，这样 camera 的内容将会渲染到新建的 RenderTexture 中。
        let texture = new cc.RenderTexture();
        // let gl = cc.game["_renderContext"];
        let width = 960;
        let height = 640;

        

        // 如果截图内容中不包含 Mask 组件，可以不用传递第三个参数
        texture.initWithSize(width, height);
        camera.targetTexture = texture;
        // 渲染一次摄像机，即更新一次内容到 RenderTexture 中
        camera["render"]();
        // // 这样我们就能从 RenderTexture 中获取到数据了
        // let data = texture.readPixels();

        // let texture = new cc.RenderTexture();
        //texture.initWithSize(width, height);
        let spriteFrame = new cc.SpriteFrame();
        let zoom = 0.025;
        let winp:cc.Size = cc.size(cc.winSize.width / zoom, cc.winSize.height / zoom);
        //spriteFrame.setTexture(texture, cc.rect(0, 0, 9600 * zoom,  9600 * zoom))
        spriteFrame.setTexture(texture);

        spriteFrame.setRect(cc.rect(cc.winSize.width / 2, -cc.winSize.height / 2, 9600 * zoom, 9600 * zoom))
        this.box.spriteFrame = spriteFrame;
        this.box.node.color = cc.Color.RED;
        this.box.node.scaleY = -1;
        this.node.destroy();
        return
        console.log(data.length)

        let list: any[] = [];
        for (let i = 0, len = data.length; i < len; i++) {
            let id = data[i];
            if (id) {
                list.push(id ? 255 : 0);
            }
        }
        console.log(list);

        cc.find("Canvas/box").getComponent(cc.Sprite).spriteFrame = new cc.SpriteFrame(ture);
        console.log()



        console.log(data);
        // 接下来就可以对这些数据进行操作了
        let canvas = document.createElement('canvas');
        let ctx = canvas.getContext('2d');
        canvas.width = texture.width;
        canvas.height = texture.height;
        console.log(width)
        console.log(height)


        // var imgData=ctx.createImageData(width,height);

        // for (let i = 0, len = data.length; i < len; i++) {
        //     let id = data[i];
        //     imgData[i] = id ? 255 :0;
        // }
        // ctx.putImageData(imgData,10,10);
        // let rowBytes = width * 4;
        // for (let row = 0; row < height; row++) {
        //     let srow = height - 1 - row;
        //     let imageData = ctx.createImageData(width, 1);
        //     let start = srow * width * 4;
        //     for (let i = 0; i < width; i += 4) {
        //         let i0 = start + i;
        //         let i1 = start + i + 1;
        //         let i2 = start + i + 2;
        //         let i3 = start + i + 3;

        //         let da = (data[i0] || data[i1] || data[i2]) ? 255 : 0;

        //         // if(id){
        //         //     id = 155;
        //         // }
        //         imageData.data[i] = da;
        //         imageData.data[i + 1] = da;
        //         imageData.data[i + 2] = da;
        //         imageData.data[i + 3] = 255;

        //     }
        //     ctx.putImageData(imageData, 0, row);
        // }


        var imgData = ctx.createImageData(width, height);
        for (let i = 0, len = imgData.data.length; i < len; i += 4) {
            {
                let i0 = i;
                let i1 = i + 1;
                let i2 = i + 2;
                let i3 = i + 3;
                let rgb = (data[i0] || data[i1] || data[i2]) ? 155 : 0;

                imgData.data[i + 0] = rgb;
                imgData.data[i + 1] = rgb;
                imgData.data[i + 2] = rgb;
                imgData.data[i + 3] = 255;
            }
        }

        ctx.putImageData(imgData, 0, 0);
        let dataURL = canvas.toDataURL("image/jpeg");

        let img = document.createElement("img");
        img.src = dataURL;
        document.body.appendChild(img);
        console.log(img);
        this.node.active = false;
    }
    // update (dt) {}
}
